package com.hq.fxh.No4_XiaoXi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.No4_XiaoXi.IM.Ladapte_imlist;
import com.hq.fxh.No4_XiaoXi.IM.LiaoTianActivity;
import com.hq.fxh.No4_XiaoXi.PingLun.AllCommentActivity;
import com.hq.fxh.No4_XiaoXi.Tongzhi.TongZhiActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;

import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/14
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_XiaoXi_KuangJia extends Fragment implements View.OnClickListener {

    protected View rootView;
    protected LinearLayout layoutPingLun;
    protected LinearLayout layoutTongZhi;
    protected LinearLayout layoutJuBao;
    protected PullToRefreshListView listIM;
    protected ImageView imageXiaoxiPinglun;
    protected ImageView imageXiaoxiTongzhi;
    protected ImageView imageXiaoxiJubao;
    private View inflate;
    Ladapte_imlist adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_xiaoxi_kuangjia, null);
        initView(rootView);

        initData();

        return rootView;
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.layout_PingLun) {
            startActivity(new Intent(getContext(), AllCommentActivity.class));
        } else if (view.getId() == R.id.layout_TongZhi) {
            startActivity(new Intent(getContext(), TongZhiActivity.class));
        } else if (view.getId() == R.id.layout_JuBao) {
            getImHuiHUa();
        }
    }

    private void initView(View rootView) {
        listIM = (PullToRefreshListView) rootView.findViewById(R.id.list_IM);
        inflate = LayoutInflater.from(getContext()).inflate(R.layout.top_xiaoxi, null);
        layoutPingLun = (LinearLayout) inflate.findViewById(R.id.layout_PingLun);
        layoutPingLun.setOnClickListener(Fragment_XiaoXi_KuangJia.this);
        layoutTongZhi = (LinearLayout) inflate.findViewById(R.id.layout_TongZhi);
        layoutTongZhi.setOnClickListener(Fragment_XiaoXi_KuangJia.this);
        layoutJuBao = (LinearLayout) inflate.findViewById(R.id.layout_JuBao);
        layoutJuBao.setOnClickListener(Fragment_XiaoXi_KuangJia.this);
        imageXiaoxiPinglun = (ImageView) inflate.findViewById(R.id.image_xiaoxi_pinglun);
        imageXiaoxiTongzhi = (ImageView) inflate.findViewById(R.id.image_xiaoxi_tongzhi);
        imageXiaoxiJubao = (ImageView) inflate.findViewById(R.id.image_xiaoxi_jubao);
    }

    private void initData() {

        listIM.setMode(PullToRefreshBase.Mode.BOTH);
        listIM.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                doInBackGroung();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                doInBackGroung();
            }
        });

        if (adapter == null) {
            adapter = new Ladapte_imlist();
        }
        adapter.setData(null);
        listIM.setAdapter(adapter);
        listIM.getRefreshableView().addHeaderView(inflate);
        listIM.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Conversation itemAtPosition = (Conversation) adapterView.getItemAtPosition(i);
                toLiaoTianActivity(itemAtPosition);
            }
        });

        getImHuiHUa();
    }


    public void toLiaoTianActivity(Conversation itemAtPosition) {
        if (itemAtPosition == null) {
            LogUtils.startToash(getContext(), " 未获取到该聊天对象");
        } else {
            UserInfo targetInfo = (UserInfo) itemAtPosition.getTargetInfo();
            if (itemAtPosition == null) {
            } else {
                Intent intent = new Intent(getContext(), LiaoTianActivity.class);
                intent.putExtra("user", targetInfo.getUserName());
                startActivity(intent);
            }
        }

    }

    public void doInBackGroung() {
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();
        listIM.onRefreshComplete();

    }

    public void getImHuiHUa() {
        UserInfo myInfo = JMessageClient.getMyInfo();
        if (myInfo == null) {
            LogUtils.startToash(getContext(), " 您的聊天功能尚未激活");
            LogUtils.e(" 您的聊天功能尚未激活");

        } else {
            List<Conversation> conversationList = JMessageClient.getConversationList();
            if (conversationList == null) {
                LogUtils.startToash(getContext(), " 未获取到聊天信息");
                LogUtils.e(" 未获取到聊天信息");
                return;
            } else {
                if (adapter == null) {
                    adapter = new Ladapte_imlist();
                }
                adapter.setData(conversationList);
                adapter.notifyDataSetChanged();
            }
        }


    }

    @Override
    public void onResume() {
        super.onResume();
        getImHuiHUa();
    }
}
